import Footer from '@/components/Footer';
import { listChartByPageUsingPost } from '@/services/gucheng/chartController';
import { getLoginUserUsingGet, userLoginUsingPost } from '@/services/gucheng/userController';
import { Link } from '@@/exports';
import { LockOutlined, UserOutlined } from '@ant-design/icons';
import { LoginForm, ProFormText } from '@ant-design/pro-components';
import { useEmotionCss } from '@ant-design/use-emotion-css';
import { Helmet, history, useModel } from '@umijs/max';
import { message, Tabs } from 'antd';
import React, { useEffect, useState } from 'react';
import { flushSync } from 'react-dom';
import Settings from '../../../../config/defaultSettings';
const Login = () => {
    const [type, setType] = useState('account');
    const { initialState, setInitialState } = useModel('@@initialState');
    const containerClassName = useEmotionCss(() => {
        return {
            display: 'flex',
            flexDirection: 'column',
            height: '100vh',
            overflow: 'auto',
            backgroundImage: "url('https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/V-_oS6r-i7wAAAAAAAAAAAAAFl94AQBr')",
            backgroundSize: '100% 100%',
        };
    });
    useEffect(() => {
        listChartByPageUsingPost({}).then((res) => {
            console.error('res', res);
        });
    });
    /**
     * 登陆成功后，获取用户登录信息
     */
    const fetchUserInfo = async () => {
        const userInfo = await getLoginUserUsingGet();
        if (userInfo) {
            flushSync(() => {
                setInitialState((s) => ({
                    ...s,
                    currentUser: userInfo,
                }));
            });
        }
    };
    const handleSubmit = async (values) => {
        try {
            // 登录
            const res = await userLoginUsingPost(values);
            if (res.code === 0) {
                const defaultLoginSuccessMessage = '登录成功！';
                message.success(defaultLoginSuccessMessage);
                await fetchUserInfo();
                const urlParams = new URL(window.location.href).searchParams;
                history.push(urlParams.get('redirect') || '/');
                return;
            }
            else {
                message.error(res.message);
            }
        }
        catch (error) {
            const defaultLoginFailureMessage = '登录失败，请重试！';
            console.log(error);
            message.error(defaultLoginFailureMessage);
        }
    };
    return (<div className={containerClassName}>
      <Helmet>
        <title>
          {'登录'}- {Settings.title}
        </title>
      </Helmet>
      <div style={{
            flex: '1',
            padding: '32px 0',
        }}>
        <LoginForm contentStyle={{
            minWidth: 280,
            maxWidth: '75vw',
        }} logo={<img alt="logo" src="/logo.svg"/>} title="GG智能 BI" subTitle={<a href="https://yupi.icu" target="_blank" rel="noreferrer">
              一个产品数据智能分析的网站
            </a>} onFinish={async (values) => {
            await handleSubmit(values);
        }}>
          <Tabs activeKey={type} onChange={setType} centered items={[
            {
                key: 'account',
                label: '账户密码登录',
            },
        ]}/>
          {type === 'account' && (<>
              <ProFormText name="userAccount" fieldProps={{
                size: 'large',
                prefix: <UserOutlined />,
            }} placeholder={'请输入用户名'} rules={[
                {
                    required: true,
                    message: '用户名是必填项！',
                },
            ]}/>
              <ProFormText.Password name="userPassword" fieldProps={{
                size: 'large',
                prefix: <LockOutlined />,
            }} placeholder={'请输入密码'} rules={[
                {
                    required: true,
                    message: '密码是必填项！',
                },
            ]}/>
            </>)}
          <div style={{
            marginBottom: 24,
        }}>
            <Link to="/user/register">注册</Link>
          </div>
        </LoginForm>
      </div>
      <Footer />
    </div>);
};
export default Login;
//# sourceMappingURL=index.jsx.map